package com.forestar.xzk.threadFor;

import android.os.Handler;
import android.os.Message;

import androidx.annotation.NonNull;

import com.forestar.xzk.appcommon.CpuUtils;
import com.forestar.xzk.logs.LogTag;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * created by  xzk
 * 2020/7/23   18:25
 * 说明:  线程切换工具类
 *      子线程工作
 *      子线程做完，主线程回调
 */
public class ThreadSwitchUtil {

    /** 线程池子 ，支持执行大量任务  **/
    private static ExecutorService serviceBg = new ThreadPoolExecutor(0, getCpuNUmber() * 4,
            60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());

    /**
     * 线程池执行大量任务
     * @param runnable
     */
    public static void execute(Runnable runnable){
        serviceBg.execute(runnable);
        int threadCount = ((ThreadPoolExecutor)serviceBg).getActiveCount();
        LogTag.e("===threadCount===", threadCount+"");
    }

    private static int getCpuNUmber() {
        int value = CpuUtils.getCoreNumber();
        if (value < 2) value = 2;
        return value;
    }
    /**
     * 子线程执行任务:少量任务
     * @param childThreadListener
     */
    public static final void runOnChildThread(final ThreadTask childThreadListener){
        Thread childThread = new Thread(){
            @Override
            public void run() {
                super.run();
                if(null != childThreadListener){
                    childThreadListener.doOnThread();
                }
            }
        };
        childThread.start();
    }
    /**
     * 子线程运行，主线程取结果，也可以不传子线程回调，直接在主线程取结果
     * @param childThreadListener
     * @param uiThreadListener
     */
    public static void runOnChildToUIThread(final ThreadTask childThreadListener, final UITask uiThreadListener){
        Thread childThread = new Thread(){
            @Override
            public void run() {
                super.run();
                if(null != childThreadListener){
                    childThreadListener.doOnThread();
                }
                if(null != uiThreadListener){
                    Message message = handler.obtainMessage(1, uiThreadListener);
                    handler.sendMessage(message);
                }
            }
        };
        childThread.start();
    }
    /**
     *  直接在主线程运行
     * @param uiThreadListener
     */
    public static void runOnUIThread( final UITask uiThreadListener){
        if(null != uiThreadListener){
            Message message = handler.obtainMessage(1, uiThreadListener);
            handler.sendMessage(message);
        }
    }

    /**
     * 执行消息回调
     */
    private static Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            Object listener = msg.obj;
            //由于回调对象是一一对应的，所以，不存在说对象调用串了的问题
            if(listener instanceof UITask){
                UITask uiThreadListener = (UITask) listener;
                uiThreadListener.doOnUI();
            }
        }
    };

    /**
     * 子线程运行回调
     */
    public interface ThreadTask{
        void doOnThread();
    }

    /**
     * 主线程运行回调
     */
    public interface UITask{
        void doOnUI();
    }
}
